#define _CRT_SECURE_NO_WARNINGS	

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
int Count(ListNode* head)
{
    int count = 0;
    ListNode* pcur = head;
    while (pcur)
    {
        count++;
        pcur = pcur->next;
    }
    return count;
}
ListNode* Find(ListNode* phead1, ListNode* phead2, int gap)
{
    ListNode* pcur1 = phead1;
    ListNode* pcur2 = phead2;
    while (gap--)
    {
        pcur2 = pcur2->next;
    }
    while (pcur1 && pcur2)
    {
        if (pcur1 == pcur2)
            return pcur1;
        pcur1 = pcur1->next;
        pcur2 = pcur2->next;
    }
    return NULL;
}
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
    int size1 = Count(headA);
    int size2 = Count(headB);
    ListNode* ret;
    if (size1 >= size2)
        ret = Find(headB, headA, size1 - size2);
    else
        ret = Find(headA, headB, size2 - size1);
    return ret;
}